<template>
  <div class="div_box">
    <div style="min-width: 1600px;">
      <el-form :inline="true" :model="searchResourceForm" @keyup.enter.native="getResourceList()">
        <el-form-item>
          <el-input v-model="searchResourceForm.name" placeholder="文件名称" title="模糊匹配" clearable />
        </el-form-item>
        <el-form-item>
          <el-input v-model="searchResourceForm.id" placeholder="文件ID" title="精确查找" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getResourceListByCondition()">查询</el-button>
          <el-button type="primary" @click="uploadScriptHandle()">上传文件</el-button>
          <el-button type="danger" :disabled="resourceListSelections.length <= 0" @click="deleteBatchHandle()">批量删除</el-button>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="resourceListLoading"
        :data="resourceList"
        style="width: 100%;"
        border
        @selection-change="selectionChangeHandle"
      >
        <el-table-column type="selection" header-align="center" align="center" width="50" />
        <el-table-column prop="id" header-align="center" align="center" width="300" label="ID" />

        <el-table-column prop="name" header-align="center" align="center" label="文件名称" />
        <el-table-column prop="size" header-align="center" align="center" label="文件大小（KB）" />
        <el-table-column prop="creator" header-align="center" align="center" label="编辑人" />
        <el-table-column prop="modifyTime" header-align="center" align="center" label="编辑时间" />
        <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" title="最多预览5MB的内容" @click="preViewHandle(scope.row)">预览</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
      />
      <!-- 上传脚本 -->
      <resource-upload v-if="uploadVisible" ref="resourceUpload" @refreshDataList="getResourceList()" />
      <!-- 查看脚本 -->
      <resource-pre-view v-if="preViewVisible" ref="resourcePreView" @refreshDataList="getResourceList()" />
    </div>
  </div>
</template>

<script>
import { deleteResourse, searchAll } from '@/api/perf/resource.js'
import ResourceUpload from './resource-upload'
import ResourcePreView from './resource-pre-view'
export default {
  name: 'PerfResource',
  components: {
    ResourceUpload,
    ResourcePreView
  },
  data() {
    return {
      // Resource Table数据
      resourceList: [],
      // 查询Form数据
      searchResourceForm: {
        name: '',
        id: ''
      },
      // Table的v-loading
      resourceListLoading: false,
      // Table中的多选，通过selectionChangeHandle方法添加或删除
      resourceListSelections: [],
      // 分页查询初始化数据
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      // 控制外部vue模块的显示
      uploadVisible: false,
      preViewVisible: false
    }
  },
  created() {
    // 初始化查询
    this.getResourceList()
  },
  activated() {
    // 初始化查询
    this.getResourceList()
  },
  methods: {
    // 删除脚本
    deleteHandle(row) {
      deleteResourse(row.id).then(respnose => {
        if (respnose && respnose.code === 200) {
          this.$message.success(respnose.message)
        } else {
          this.$message.error(respnose.message)
        }
        this.getResourceList()
      })
    },
    // 查询resource列表
    getResourceList() {
      this.resourceListLoading = true
      searchAll(this.pageIndex, this.pageSize).then(respnose => {
        if (respnose && respnose.code === 200) {
          const { data } = respnose
          this.resourceList = data.content
          this.totalPage = data.totalElements
        } else {
          this.$message.error(respnose.message)
          this.resourceList = []
          this.totalPage = 0
        }
        this.resourceListLoading = false
      })
    },
    getResourceListByCondition() {
      this.getResourceList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getResourceList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getResourceList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.resourceListSelections = val
    },
    // 删除
    deleteBatchHandle(id) {
      var ids = id ? [id] : this.resourceListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/renren-fast/perf/resource/deleteBatch'), // todo
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({
          respnose
        }) => {
          if (respnose && respnose.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getResourceList()
              }
            })
          } else {
            this.$message.error(respnose.message)
          }
        })
      }).catch(() => {})
    },
    // 查看脚本
    preViewHandle(row) {
      this.preViewVisible = true
      this.$nextTick(() => {
        this.$refs.resourcePreView.init(row.id)
      })
    },
    // 上传脚本
    uploadScriptHandle() {
      this.uploadVisible = true
      this.$nextTick(() => {
        this.$refs.resourceUpload.init()
      })
    }
  }
}
</script>

<style scoped="scoped">
.div_box {
  border: 1px solid #eee;
  padding-left: 15px;
  padding-right: 15px;
  padding-top: 15px;
  padding-bottom: 10px;
  margin-top: 10px;
  margin-bottom: 10px;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  overflow-x: auto;
}
</style>
